﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Month
{
    /// <summary>
    /// 小组月均值
    /// </summary>
    public class ServiceGroupAvgMonth : Benefit.Interface.Month.IGroupAvgMonth
    {
        Benefit.DB.DBManager db = null;
        public ServiceGroupAvgMonth(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当月所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int month, int year)
        {
            var query = from t in db.GroupAvgMonth where t.Year == year && t.Month == month select t;
            foreach (Models.Month.GroupAvgMonth c in query)
            {
                db.GroupAvgMonth.Remove(c);
            }
            db.SaveChanges();
        }


        public Models.Month.GroupAvgMonth GetGroupAvgMonth(int groupId, int month, int year)
        {
            var query = db.GroupAvgMonth.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 保存月小组均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitGroupMonthAvg(int month, int year)
        {
            List<Models.Sys.Groups> groups = db.Groups.ToList();
            foreach (Models.Sys.Groups g in groups)
            {
                Models.Month.GroupAvgMonth day = new Models.Month.GroupAvgMonth();
                day.GroupId = g.Id;
                day.Month = month;
                day.Year = year;

                var queryExist = from t in db.GroupAvgMonth where t.Month == month && t.Year == year && t.GroupId == g.Id select t;
                if (queryExist.Count() > 0)//有过当月的记录
                {
                    day = queryExist.First();//找到这条记录
                }

                //dfdsfdsafdsafdsdsffsdfds这一段不对
                var query = from t in db.GroupAvgDay where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.GroupId == g.Id select t;
                if (query.Count() > 0)
                {
                    day.BillCount = Math.Round(query.Average(a => a.BillCount), 2);
                    day.DayCount = Math.Round(query.Average(a => a.DayCount), 2);
                    day.Free = Math.Round(query.Average(a => a.Free), 2);
                    day.LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
                    day.Profit = Math.Round(query.Average(a => a.Profit), 2);
                    day.SumLost = Math.Round(query.Average(a => a.SumLost), 2);
                    day.SumWin = Math.Round(query.Average(a => a.SumWin), 2);
                    day.UsedMargin = Math.Round(query.Average(a => a.UsedMargin), 2);
                    day.WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
                    day.AvgBillBenefit = Math.Round(query.Average(a => a.AvgBillBenefit), 2);
                    day.AvgPostionTime = Math.Round(query.Average(a => a.AvgPostionTime), 2);
                    day.MaxReturn = Math.Round(query.Average(a => a.MaxReturn), 2);
                    day.OverStopLoss = Math.Round(query.Average(a => a.OverStopLoss), 2);
                    day.PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
                    day.RaiseRate = Math.Round(query.Average(a => a.RaiseRate), 2);
                    day.RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
                    day.Utilization = Math.Round(query.Average(a => a.Utilization), 2);
                    day.WinRate = Math.Round(query.Average(a => a.WinRate), 2);
                    day.Yield = Math.Round(query.Average(a => a.Yield), 2);

                }
                if (queryExist.Count() == 0)
                {
                    db.GroupAvgMonth.Add(day);
                }
            }
            db.SaveChanges();
        }
    }
}